import OrgAPI from "@/api/org/org";
import { useTableData } from "@/components/SmTable/src/utils";

export const useTable = () => {
  /**
  2-报案信息录入

  2-查询
  1-报案号-field1
  2-车牌号-field2
  3-车架号-field3
  4-被统筹人
  5-案件状态-下拉
  6-承统机构-下拉
  7-统筹单位-下拉
  8-出险时间


  3-列表
  1-报案时间
  2-报案号
  3-车牌号
  4-被统筹人
  5-承统机构
  6-承统单位
  7-出险时间
  8-联系电话
  9-等待时长
  10-案件状态
  11-案件进展
  11-操作
  修改-查看操作记录-结案(不发短信)-结案(发短信)-撤销

 */

  const list: any = ref([]);
  const total = ref(0);
  const loading = ref(false);

  const { tableOptions, transformTableOptions } = useTableData();

  const fieldMap = {
    field1: "报案时间",
    field2: "打报案号",
    field3: "车牌号",
    field4: "被统筹人",
    field5: "承统机构",
    field6: "承统单位",
    field7: "出险时间",
    field8: "联系电话",
    field9: "等待时长",
    field10: "案件状态",
    field11: "案件进展",
    operate: {
      label: "操作",
      slot: "operate",
      fixed: "right",
      width: "180px",
    },
  };
  tableOptions.value = transformTableOptions(fieldMap);

  const queryParams: any = reactive({
    pageNum: 1,
    pageSize: 10,
  });

  /** 查询列表 */
  const getList = async () => {
    loading.value = true;
    try {
      const data = await OrgAPI.getList(queryParams);
      list.value = data.list;
      console.log(data, "data-data");
      total.value = data.total;
    } finally {
      loading.value = false;
    }
  };

  function handleQuery() {
    queryParams.pageNum = 1;
    getList();
  }

  const queryFormRef = ref();
  /** 添加/修改操作 */
  const formRef = ref();
  const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id);
  };

  /** 重置按钮操作 */
  const handleResetQuery = () => {
    queryFormRef.value.resetFields();
    nextTick(() => {
      handleQuery();
    });
  };

  return {
    tableOptions,
    list,
    total,
    loading,
    queryParams,
    getList,
    handleQuery,
    handleResetQuery,
    queryFormRef,
    formRef,
    openForm,
  };
};
